import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import useRouterStore from '../store/modules/router.store.ts';
// 1. 配置路由
const routes: Array<RouteRecordRaw> = [
  {
    path: '/', // 默认路由 home页面
    meta: { title: '首页' },
    component: () => import('../pages/index/Index.vue')
  },
  {
    path: '/article-detail/:id',
    meta: { title: '文章详情' },
    component: () => import('@/pages/index/view/ViewArticle.vue')
  },
  {
    path: '/talk', // 默认路由 home页面
    meta: { title: '说说' },
    component: () => import('../pages/talk/Talk.vue')
  },
  {
    path: '/talk-detail/:id',
    meta: { title: '说说详情' },
    component: () => import('@/pages/talk/view/TalkView.vue')
  },
  {
    path: '/album', // 默认路由 home页面
    meta: { title: '相册' },
    component: () => import('../pages/album/Album.vue')
  },
  {
    path: '/album-detail/:id', // 默认路由 home页面
    meta: { title: '相册详情' },
    component: () => import('../pages/album/album-list/AlbumList.vue')
  }
];
// 2.返回一个 router 实列，为函数，配置 history 模式
const router = createRouter({
  history: createWebHashHistory(),
  routes
});

let timer: any;

router.beforeEach((to, _, next) => {
  clearTimeout(timer);
  // 在每次路由切换前获取当前的路由地址
  const currentPath = to.path;
  useRouterStore().setCurrentPath(currentPath);
  useRouterStore().setIsInRouter(true);
  document.title = `${to.matched[0].meta.title}`;
  next();
});

router.afterEach(() => {
  timer = setTimeout(() => {
    useRouterStore().setIsInRouter(false);
  }, 600);
});

// 3.导出路由   去 main.ts 注册 router.ts
export default router;
